<?php

namespace app\admin\controller\mall;

use app\admin\traits\Curd;
use app\common\controller\AdminController;
use app\common\model\MallOrder;
use app\common\model\MallUser;
use think\App;

class Order extends AdminController
{
    use Curd;
    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->model =(new MallOrder());
    }

    /**
     * @NodeAnotation(title="编辑")
     */
    public function edit($id)
    {
        $row = $this->model->find($id);
        empty($row) && $this->error('数据不存在');
        if ($this->request->isPost()) {
            $post = $this->request->post();
            $rule = [];
            $this->validate($post, $rule);

            if($post['status'] ==1){
                if(empty($post['orderprice']) || empty($post['rate']) || empty($post['cycle']) || empty($post['repayment'])){
                    $this->error('请填写贷款金额/周期/利率/还款计划');
                }
            }else if($post['status'] ==2){
                if(empty($post['reason'])){
                    $this->error('请填写审核失败理由');
                }
            }else{
                $this->error('请选择审核状态');
            }

            try {
                $save = $row->save($post);
            } catch (\Exception $e) {
                $this->error('保存失败');
            }
            // 通过审核放款，计算佣金
            if($post['status']==1){
                $invite=(new Malluser)->alias('u')->field('m.*')
                    ->join('mall_user m','m.invite_code=u.parent_code')
                    ->where([['u.id','=',$row['user_id']]])
                    ->find();
                if($invite){
                    (new MallUser())->where([['id','=',$invite['id']]])->Inc('balance',$post['commission'])->update();
                    (new MallUser())->where([['id','=',$invite['id']]])->Inc('income',$post['commission'])->update();
                }

            }
            $save ? $this->success('保存成功') : $this->error('保存失败');
        }

        if($row['orderprice'] == 0){
            $row['orderprice']='';
        }
        if($row['cycle'] == 0){
            $row['cycle']='';
        }
        $row['repayment'] =htmlspecialchars_decode($row['repayment']);
        $row['reason'] =htmlspecialchars_decode($row['reason']);
        $this->assign('row', $row);
        return $this->fetch();
    }


    /**
     * 用户认证信息
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function info($id){
        $rowinfo = $this->model->find($id);

        empty($rowinfo) && $this->error('数据不存在');
        $row =(new MallUserCertification())->where([["user_id",'=',$rowinfo['user_id']]])->find();

        $row['workdesc'] =json_decode($row['workdesc'],true);
        $row['housedesc'] =json_decode($row['housedesc'],true);
        $row['cardesc'] =json_decode($row['cardesc'],true);
        $row['insurancedesc'] =json_decode($row['insurancedesc'],true);
        $row['commonisdesc'] =json_decode($row['commonisdesc'],true);
        $row['welfaredesc'] =json_decode($row['welfaredesc'],true);

        $this->assign('row', $row);
        return $this->fetch();
    }
}